package springboot.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import springboot.user.mapper.UserMapper;
import springboot.user.model.User;
import springboot.user.model.UserExample;
import springboot.user.model.UserExample.Criteria;
import springboot.user.service.UserService;
import springboot.util.BlankUtil;

@Service
public class UserServiceImpl implements UserService {

	@Resource
	private UserMapper userMapper;

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		userMapper.insert(user);
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		userMapper.updateByPrimaryKey(user);
	}

	@Override
	public void deleteUser(Integer id) {
		// TODO Auto-generated method stub
		userMapper.deleteByPrimaryKey(id);
	}

	@Override
	public User getUserById(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<User> getUser() {
		// TODO Auto-generated method stub
		return userMapper.selectByExample(null);
	}

	@Override
	public List<User> getUserByUsernameAndName(String username, String name) {
		
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		/*if (username!=null && username.trim().length()!=0) {
			criteria.andUsernameEqualTo(username);
		}
		
		if (name!=null && name.trim().length()!=0) {
			criteria.andNameLike("%"+name+"%");
		}*/
		
		if (BlankUtil.isNoBlank(username)) {
			criteria.andUsernameEqualTo(username);
		}
		
		if (BlankUtil.isNoBlank(name)) {
			criteria.andNameLike("%"+name+"%");
		}
		
		return userMapper.selectByExample(example);
	}

	
}
